"""添加logs表名和列名描述

Revision ID: e850c5a37d99
Revises: ca0a1aab287e
Create Date: 2025-11-17 13:26:44.433120

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'e850c5a37d99'
down_revision: Union[str, Sequence[str], None] = 'ca0a1aab287e'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('logs', 'request_id',
               existing_type=sa.UUID(),
               comment='请求唯一ID',
               existing_nullable=False)
    op.alter_column('logs', 'ip',
               existing_type=sa.VARCHAR(length=50),
               comment='请求IP',
               existing_nullable=True)
    op.alter_column('logs', 'method',
               existing_type=sa.VARCHAR(length=10),
               comment='请求方法',
               existing_nullable=True)
    op.alter_column('logs', 'url',
               existing_type=sa.VARCHAR(length=200),
               comment='请求URL',
               existing_nullable=True)
    op.alter_column('logs', 'path_params',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment='路径参数',
               existing_nullable=True)
    op.alter_column('logs', 'query_params',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment='查询参数',
               existing_nullable=True)
    op.alter_column('logs', 'request_body',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment='请求体',
               existing_nullable=True)
    op.alter_column('logs', 'status_code',
               existing_type=sa.INTEGER(),
               comment='响应状态码',
               existing_nullable=True)
    op.alter_column('logs', 'response_body',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment='响应体',
               existing_nullable=True)
    op.alter_column('logs', 'process_time',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               comment='请求耗时（ms）',
               existing_nullable=True)
    op.create_index(op.f('ix_logs_request_id'), 'logs', ['request_id'], unique=False)
    op.create_table_comment(
        'logs',
        'API请求日志',
        existing_comment=None,
        schema=None
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table_comment(
        'logs',
        existing_comment='API请求日志',
        schema=None
    )
    op.drop_index(op.f('ix_logs_request_id'), table_name='logs')
    op.alter_column('logs', 'process_time',
               existing_type=sa.DOUBLE_PRECISION(precision=53),
               comment=None,
               existing_comment='请求耗时（ms）',
               existing_nullable=True)
    op.alter_column('logs', 'response_body',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment=None,
               existing_comment='响应体',
               existing_nullable=True)
    op.alter_column('logs', 'status_code',
               existing_type=sa.INTEGER(),
               comment=None,
               existing_comment='响应状态码',
               existing_nullable=True)
    op.alter_column('logs', 'request_body',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment=None,
               existing_comment='请求体',
               existing_nullable=True)
    op.alter_column('logs', 'query_params',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment=None,
               existing_comment='查询参数',
               existing_nullable=True)
    op.alter_column('logs', 'path_params',
               existing_type=postgresql.JSON(astext_type=sa.Text()),
               comment=None,
               existing_comment='路径参数',
               existing_nullable=True)
    op.alter_column('logs', 'url',
               existing_type=sa.VARCHAR(length=200),
               comment=None,
               existing_comment='请求URL',
               existing_nullable=True)
    op.alter_column('logs', 'method',
               existing_type=sa.VARCHAR(length=10),
               comment=None,
               existing_comment='请求方法',
               existing_nullable=True)
    op.alter_column('logs', 'ip',
               existing_type=sa.VARCHAR(length=50),
               comment=None,
               existing_comment='请求IP',
               existing_nullable=True)
    op.alter_column('logs', 'request_id',
               existing_type=sa.UUID(),
               comment=None,
               existing_comment='请求唯一ID',
               existing_nullable=False)
    # ### end Alembic commands ###
